home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / lib / scandir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-04  |  2.2 KB  |  94 lines

  1. #include "../config.h"
  2. #ifndef HAVE_SCANDIR
  3. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  4.  
  5. /*
  6. **  SCANDIR
  7. **  Scan a directory, collecting all (selected) items into a an array.
  8. */
  9.  
  10. #include "../ir/cdialect.h"
  11. /* #include <sys/types.h> */
  12. #include <stdio.h>
  13. #include "pdftw.h"
  14.  
  15. #ifdef    RCSID
  16. static char RCS[] = "$Header: /usr/local/ls6/src+data/src/freeWAIS-sf/lib/RCS/scandir.c,v 1.2 1994/08/05 07:13:44 pfeifer Exp $";
  17. #endif    /* RCSID */
  18.  
  19. /* Initial guess at directory size. */
  20. #define INITIAL_SIZE    20
  21.  
  22. /*
  23. extern char        *malloc();
  24. extern char        *realloc();
  25. extern char        *strcpy();
  26. */
  27. int
  28. scandir(name, list, selector, sorter)
  29.     char          *name;
  30.     struct dirent        ***list;
  31.     int             (*selector)();
  32.     int             (*sorter)();
  33. {
  34.     register struct dirent      **names;
  35.     register struct dirent      *entp;
  36.     register DIR      *dirp;
  37.     register int       i;
  38.     register int       size;
  39.  
  40.     /* Get initial list space and open directory. */
  41.     size = INITIAL_SIZE;
  42.     names = (struct dirent **)malloc(size * sizeof names[0]);
  43.     if (names == NULL)
  44.     return -1;
  45.     dirp = opendir(name);
  46.     if (dirp == NULL)
  47.     return -1;
  48.  
  49.     /* Read entries in the directory. */
  50.     for (i = 0; entp = readdir(dirp); )
  51.     if (selector == NULL || (*selector)(entp)) {
  52.         /* User wants them all, or he wants this one. */
  53.         if (++i >= size) {
  54.         size <<= 1;
  55.         names = (struct dirent **)
  56.             realloc((char *)names, size * sizeof names[0]);
  57.         if (names == NULL) {
  58.             closedir(dirp);
  59.             return -1;
  60.         }
  61.         }
  62.  
  63.         /* Copy the entry. */
  64. #ifdef DIRSIZ
  65.         names[i - 1] = (struct dirent *)malloc(DIRSIZ(entp));
  66. #else
  67.         names[i - 1] = (struct dirent *)malloc(sizeof(struct dirent) 
  68.                             + strlen(entp->d_name)+1);
  69. #endif
  70.         if (names[i - 1] == NULL) {
  71.         closedir(dirp);
  72.         return -1;
  73.         }
  74.         names[i - 1]->d_ino = entp->d_ino;
  75.         names[i - 1]->d_reclen = entp->d_reclen;
  76. #ifndef DIRENT
  77.         names[i - 1]->d_namlen = entp->d_namlen;
  78. #endif
  79.         (void)strcpy(names[i - 1]->d_name, entp->d_name);
  80.     }
  81.  
  82.     /* Close things off. */
  83.     names[i] = NULL;
  84.     *list = names;
  85.     closedir(dirp);
  86.  
  87.     /* Sort? */
  88.     if (i && sorter)
  89.     qsort((char *)names, i, sizeof names[0], sorter);
  90.  
  91.     return i;
  92. }
  93. #endif /* HAVE_SCANDIR */
  94.